package io.dshubs.archives.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dshubs.archives.dao.ArchiveSpecialFeatureMapper;
import io.dshubs.archives.dto.ArchiveBaseCategoryDto;
import io.dshubs.archives.dto.ArchiveSpecialFeatureDto;
import io.dshubs.archives.entity.ArchiveBaseCategory;
import io.dshubs.archives.entity.ArchiveSpecialFeature;
import io.dshubs.archives.service.ArchiveSpecialFeatureService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案专题表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-09-03
 */
@Slf4j
@Api(tags = "专题")
@RestController
@RequestMapping("/special-feature")
public class ArchiveSpecialFeatureController {

    @Autowired
    private ArchiveSpecialFeatureService archiveSpecialFeatureService;

    @Autowired
    private ArchiveSpecialFeatureMapper archiveSpecialFeatureMapper;


    @GetMapping("list")
    @ApiOperation("专题:list")
    @LogOperation("专题:list")
    public Result<List<ArchiveSpecialFeatureDto>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        List<ArchiveSpecialFeatureDto> list = archiveSpecialFeatureService.list(params);

        return new Result<List<ArchiveSpecialFeatureDto>>().ok(list);
    }


    @GetMapping("{id}")
    @ApiOperation("详情")
    @LogOperation("专题:get")
    public Result<ArchiveSpecialFeature> get(@PathVariable("id") Long id){
        return new Result<ArchiveSpecialFeature>().ok(archiveSpecialFeatureService.selectById(id));
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("专题:save")
    public Result save(@RequestBody ArchiveSpecialFeature archiveSpecialFeature){
        //效验数据
        ValidatorUtils.validateEntity(archiveSpecialFeature, AddGroup.class, DefaultGroup.class);
        archiveSpecialFeature.setUserId(SecurityUser.getUserId());
        archiveSpecialFeatureService.insert(archiveSpecialFeature);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("专题:update")
    public Result update(@RequestBody ArchiveSpecialFeature archiveSpecialFeature){
        //效验数据
        ValidatorUtils.validateEntity(archiveSpecialFeature, UpdateGroup.class, DefaultGroup.class);

        archiveSpecialFeatureService.updateById(archiveSpecialFeature);

        return new Result();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    @LogOperation("专题:delete")
    public Result delete(@PathVariable String id){
        ArchiveSpecialFeature archiveSpecialFeature = archiveSpecialFeatureMapper.selectById(id);
        if ( CollectionUtils.isNotEmpty(archiveSpecialFeatureMapper.selectList(new QueryWrapper<ArchiveSpecialFeature>().eq("pid", archiveSpecialFeature.getId())))
        ) {
            return new Result().error("该节点下面存在子节点不能删除");
        }
        archiveSpecialFeatureService.deleteById(id);
        return new Result();
    }
}
